<?php

namespace app\push\controller;
use workerman\Lib\Timer;
use think\Db;
use think\worker\Server;
use app\admin\controller\Settlements as settle;
use app\admin\controller\Task as tas;
use app\admin\controller\Student;

class Worker extends Server
{
    protected $socket = 'websocket://0.0.0.0:2346';

    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data)
    {
        $connection->send($data);
    }

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {
        $connection->send('你连接成功了！');
    }

    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    {
        $connection->send('已断开了连接！');
    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        $connection->send('连接错误！' . $code . ',' . $msg);
        //echo "error $code $msg\n";
    }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {
        // 每1秒执行一次
        $time_interval = 1;
        Timer::add($time_interval, function()use($worker){
            //当前时间
            $date = date('Y-m-d H:i:s');
            //每月第一天
            $firstDay = date('Y-m-01', strtotime($date));
            //每月最后一天清除结算状态
            $lastDay = date('Y-m-d 23:58:00', strtotime("$firstDay +1 month -1 day"));
            //每月最后一天开始结算时间
            $monthSettleTime = date('Y-m-d 23:59:00', strtotime("$firstDay +1 month -1 day"));
            //每月第一天凌晨整点（结算完成时间）
            $lastDays = date('Y-m-01 00:00:00');
            //每月第一天执行本月发放学币任务
            $grantCoinTime = date('Y-m-01 00:01:00');
            //Validate::gt(10,8); // true
            //定时执行任务
            if(
                $date > $lastDay &&
                $date <= $monthSettleTime
            ){
                //每月最后一天清除结算状态
                (new settle())->start_settlement();
            }elseif (
                $date > $monthSettleTime &&
                $date <= $lastDays
            ){
                //每月最后一天开始结算
                (new settle())->start_settlement();
            }elseif (
                $date > $lastDays &&
                $date <= $grantCoinTime
            ){
                //每月第一天执行本月发放学币任务
                (new Student())->month_coin();
            }
            foreach($worker->connections as $connection) {
                $connection->send();
            }
        });

    }
    /*
     * 针对uid发送推送消息
     * */
    function sendMessageByUid($uid, $message)
    {
        global $worker;
        if(isset($worker->uidConnections[$uid]))
        {
            $connection = $worker->uidConnections[$uid];
            $connection->send($message);
            return true;
        }
        return false;
    }
    // 向所有验证的用户推送数据
    function broadcast($message)
    {
        global $worker;
        foreach($worker->uidConnections as $connection)
        {
            $connection->send($message);
        }
    }
}